/**
 * My simple header template from child directory
 * Area.java - ${projectName} - ${organizationName} - ${inceptionYear}
 */
package FourthTry;

/*
 * #%L
 * Ability-tester
 * %%
 * Copyright (C) 2014 AmazingSERKU
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import FourthTry.properties.AreaPropertyHolder;

public class Area extends Realm implements IArea {

    private AreaPropertyHolder areaPropertyHolder;

    public Area(Coordinates measuresFrom, Coordinates measuresTo, AreaPropertyHolder areaPropertyHolder) {
        super(measuresFrom, measuresTo);
        this.areaPropertyHolder = areaPropertyHolder;
    }

    public Area(Coordinates measuresTo, AreaPropertyHolder areaPropertyHolder) {
        this(new Coordinates(0, 0), measuresTo, areaPropertyHolder);
    }

    /*public SubArea getSubArea(Coordinates firstCoordinates, Coordinates secondCoordinates) {
        Coordinates coordinatesFrom = new Coordinates(Math.min(firstCoordinates.getX(), secondCoordinates.getX()),
                                                      Math.min(firstCoordinates.getY(), secondCoordinates.getY()));
        Coordinates coordinatesTo = new Coordinates(Math.max(firstCoordinates.getX(), secondCoordinates.getX()),
                Math.max(firstCoordinates.getY(), secondCoordinates.getY()));
        return new SubArea(this, coordinatesFrom, coordinatesTo);
    }*/

    public Coordinates getCornerCoordinates(Direction directionX, Direction directionY) {
        int x, y;
        if(directionX.equals(Direction.UP) || directionY.equals(Direction.UP)) {
            x = measuresFrom.getX();
        } else {
            x = measuresTo.getX();
        }
        if(directionY.equals(Direction.LEFT) || directionX.equals(Direction.LEFT)) {
            y = measuresFrom.getY();
        } else {
            y = measuresTo.getY();
        }
        return new Coordinates(x, y);
    }

    public Cell getCell(Coordinates tempCoordinates) {
        Cell cell = new Cell(tempCoordinates);
        if (tempCoordinates.getX() > getMeasuresTo().getX() || tempCoordinates.getX() < getMeasuresFrom().getX()) {
            cell.setAbroad(true);
            return cell;
        }
        if (tempCoordinates.getY() > getMeasuresTo().getY() || tempCoordinates.getY() < getMeasuresFrom().getY()) {
            cell.setAbroad(true);
            return cell;
        }
        areaPropertyHolder.fillCell(cell);
        return cell;
    }
}
